package com.example.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private ImageView iv1,iv2,iv3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        //配置ARGB_8888
        one();
        //配置ARGB_4444
        two();

        //不采样加载图片
        iv1 = findViewById(R.id.iv1);
        iv2 = findViewById(R.id.iv2);
        three();
        //二次采样/尺寸压缩：减少像素点
        four();




    }

    private void four() {
        //第一次：只采集宽度和高度，不采像素
        BitmapFactory.Options options =  new BitmapFactory.Options();
        options.inJustDecodeBounds = true;//只采集宽度和高度，不采像素
        BitmapFactory.decodeResource(getResources(), R.drawable.test,options);
        int outWidth = options.outWidth;//采到宽度
        int outHeight = options.outHeight;//采到高度
        Log.d("MainActivity", "原图outWidth: "+outWidth);
        Log.d("MainActivity", "原图outHeight: "+outHeight);
        //第二次：计算采样率（原图宽高/ImageView宽高），进行二次采样，减少像素点集采
        int sampleSize = 1;
        while (outWidth / sampleSize > 150 || outHeight / sampleSize > 150) {
            sampleSize *= 2; //2 4 8 16 32 。。。。。
        }
        Log.d("MainActivity", "计算采样率: "+sampleSize);

        options.inJustDecodeBounds = false; //采像素点
        options.inSampleSize = sampleSize; //采样率
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
        Log.d("MainActivity", "二次采样大小: "+bitmap.getByteCount());
        iv2.setImageBitmap(bitmap);
    }

    private void three() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        Log.d("MainActivity", "没有采样大小: "+bitmap.getByteCount());
        iv1.setImageBitmap(bitmap);

    }

    private void two() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
        Log.d("MainActivity", "RGB_565: "+bitmap.getByteCount());
        Log.d("MainActivity", "width: "+bitmap.getWidth());
        Log.d("MainActivity", "height: "+bitmap.getHeight());
    }

    private void one() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
        Log.d("MainActivity", "ARGB_8888: "+bitmap.getByteCount());
        Log.d("MainActivity", "width: "+bitmap.getWidth());
        Log.d("MainActivity", "height: "+bitmap.getHeight());
    }
}